package com.brayan.webapp.controller.media;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Scanner;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.servlet.http.Part;

import org.apache.log4j.Logger;

import com.brayan.webapp.model.media.Album;
import com.brayan.webapp.repository.media.AlbumRepository;

//@Named
//@ConversationScoped
//@MultipartConfig

@ManagedBean
@ViewScoped
public class AlbumController implements Serializable {
	
	private static final long serialVersionUID = 8896860961649748234L;
	
	private static Logger LOGGER = Logger.getLogger(AlbumController.class);

	@Inject
	private AlbumRepository albumRepo;
	
	private Long id;
	
	private String name;
	private String description;

	private int numberOfPictures;	
	
	private String searchFilter;
	private List<Album> listOfAlbums;
	
	private Part uploadedFile;
	private String fileContent;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getNumberOfPictures() {
		return numberOfPictures;
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setNumberOfPictures(int numberOfPictures) {
		this.numberOfPictures = numberOfPictures;
	}	
	
	public String getSearchFilter() {
		return searchFilter;
	}

	public void setSearchFilter(String searchFilter) {
		this.searchFilter = searchFilter;
	}
	
	public List<Album> getListOfAlbums() {
		if(getSearchFilter() != null)
			this.listOfAlbums = albumRepo.findAlbumByString(getSearchFilter());
		else
			this.listOfAlbums = albumRepo.getAllAlbums();
		return listOfAlbums;
	}

	public void setListOfAlbums(List<Album> listOfAlbums) {
		this.listOfAlbums = listOfAlbums;
	}

	public Part getUploadedFile() {
		return uploadedFile;
	}

	public void setUploadedFile(Part uploadedFile) {
		this.uploadedFile = uploadedFile;
	}

	public String getFileContent() {
		return fileContent;
	}

	public void setFileContent(String fileContent) {
		this.fileContent = fileContent;
	}

	public List<Album> findAlbumByString(){
		return albumRepo.findAlbumByString(getSearchFilter());
	}
	
	public void upload() {
		LOGGER.info("Attempting to upload file.");
	    try {
	      setFileContent(new Scanner(uploadedFile.getInputStream()).useDelimiter("\\A").next());
	    } catch (IOException e) {
	    	LOGGER.error("Failed to read/scan the uploaded file");
	    }
	  }
	
	public void delete(Album album){
		albumRepo.delete(album.getId());
	}
	
	public void loadDetail(Album album){
		setId(album.getId());
		setName(album.getName());
		setDescription(album.getDescription());
	}
	
	public void save(){
		Album album = new Album();
		album.setId(getId());
		album.setName(getName());
		album.setDescription(getDescription());
		albumRepo.save(album);
		reset();
	}
	
	private void reset(){
		setId(null);
		setName(null);
		setDescription(null);
	}

}
